import axios from "axios";
import { Message } from "element-ui";
const contextPath = "/obpm/api";

// 默认请求的基础地址
const instance = axios.create({
  withCredentials: true, // 允许携带cookie
});

instance.interceptors.request.use(
  function(config) {
    // 判断是否需携带token
    config.headers["adminToken"] = localStorage.getItem("adminToken");

    return config;
  },
  function(error) {
    return Promise.reject(error);
  }
);

// 响应拦截器
instance.interceptors.response.use(
  (response) => {
    console.log("axios.interceptors.response");
    // 请求成功后批量处理错误信息
    if (response.data.errcode && response.data.errcode !== 0 && response.data.errmsg) Message.warning(response.data.errmsg);
    return response.data;
  },
  (error) => {
    //未登录
    if (error.response.status == 401) {
      window.location = "index.html#/login";
    } else {
      Message.error(error.msg);
      return Promise.reject(error);
    }
  }
);

// 登录
export const login = (data) => {
  return instance({
    url: "signon/runtime/login/checkin",
    method: "post",
    data,
  });
};
